package com.richapp.launcher.ui.fragments.child.power;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import com.richapp.launcher.R;
import com.richapp.launcher.ui.adapter.ChildRoomPowerGridAdapter;
import com.richapp.launcher.ui.adapter.ChildRoomPowerListAdapter;

public class PowerControlFragment extends Fragment {
    private ListView roomPowerList = null;
    private GridView roomPowerGrid = null;
    private FrameLayout power_child_arrow_up = null;
    private ChildRoomPowerGridAdapter childRoomPowerGridAdapter = null;
    private ChildRoomPowerListAdapter childRoomPowerListAdapter = null;
    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.power_control,container,false);
		init(view);
		return view;
	}

	private void init(View view){
		power_child_arrow_up  = (FrameLayout) view
				.findViewById(R.id.power_child_arrow_up);
		power_child_arrow_up.setNextFocusDownId(R.id.child_roompower_list);
		
	  	roomPowerList = (ListView)view.findViewById(R.id.child_roompower_list);
	    childRoomPowerListAdapter = new ChildRoomPowerListAdapter(getActivity());
	    roomPowerList.setAdapter(childRoomPowerListAdapter);
	  	
	  	roomPowerGrid = (GridView)view.findViewById(R.id.child_roompower_gridview);
	    childRoomPowerGridAdapter = new ChildRoomPowerGridAdapter(getActivity());
	    roomPowerGrid.setOnItemClickListener(gridItemClick);
	    roomPowerGrid.setAdapter(childRoomPowerGridAdapter);
	}
	boolean flag = false;
	OnItemClickListener gridItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			 flag = !flag;
			 ImageView power_switch = (ImageView)view.findViewById(R.id.power_switch);
			 power_switch.setTag(flag);
			 if (Boolean.parseBoolean(power_switch.getTag().toString())) {
				 power_switch.setImageResource(R.drawable.child_light_on);
			 }else {
				 power_switch.setImageResource(R.drawable.child_light_off);
			}
		}
	};
	
	public static PowerControlFragment getInstance() {
		return new PowerControlFragment();
	}
}
